#!/bin/bash

ARGS=("$@")

if [ ${#ARGS[@]} -ne 11 ]
then
	echo "Error: incorrect number of arguments."
	echo "Usage: train-and-output-latex.sh run_latex config_file ds_file ds_tree_format ds_file_format ps_file ps_tree_format ps_file_format gloss_file gloss_file_format output_dir"
	exit
fi

script_dir=$(dirname $0)
dist=$script_dir/../bin
pkg=edu.washington.ling.syntric.proc
jar_dir=$script_dir/../jars
latex_dir=$script_dir/../latex

compile_latex=${ARGS[0]}
config_file=${ARGS[1]}
ds_file=${ARGS[2]}
ds_tree_format=${ARGS[3]}
ds_file_format=${ARGS[4]}
ps_file=${ARGS[5]}
ps_tree_format=${ARGS[6]}
ps_file_format=${ARGS[7]}
gloss_file=${ARGS[8]}
gloss_file_format=${ARGS[9]}
output_dir=${ARGS[10]}

# Set up output directory
if [ ! -d $output_dir ]; then
	if ! mkdir $output_dir ; then
		echo Error: output_dir=\'$output_dir\' was not valid
		echo Please specify a valid output directory
		exit
	fi
fi

echo %%
echo %% 00 Refreshing Glosses
echo %%
if [ -d $output_dir/00-ref-gloss ]; then
	rm -rf $output_dir/00-ref-gloss
fi
mkdir $output_dir/00-ref-gloss
java -cp $dist:$jar_dir/* $pkg.RefreshGlosses $ds_file $gloss_file $output_dir/00-ref-gloss/ds-gloss.txt --0s ds --0t $ds_tree_format --0f $ds_file_format --1f sgml --2s ds --2t $ds_tree_format --2f sgml 2> $output_dir/00-ref-gloss/errors-ds.txt
java -cp $dist:$jar_dir/* $pkg.RefreshGlosses $ps_file $gloss_file $output_dir/00-ref-gloss/ps-gloss.txt --0s ps --0t $ps_tree_format --0f $ps_file_format --1f sgml --2s ps --2t $ps_tree_format --2f sgml 2> $output_dir/00-ref-gloss/errors-ps.txt

echo 
echo %%
echo %% 10 Checking DS Format
echo %%
if [ -d $output_dir/10-check-ds ]; then
	rm -rf $output_dir/10-check-ds
fi
mkdir $output_dir/10-check-ds
mkdir $output_dir/10-check-ds/latex
java -cp $dist:$jar_dir/* $pkg.CheckDSFormat $output_dir/00-ref-gloss/ds-gloss.txt $output_dir/10-check-ds/ds-good.txt $output_dir/10-check-ds/ds-bad.txt --0t $ds_tree_format --0f sgml --1f sgml --2f sgml 2> $output_dir/10-check-ds/errors.txt

java -cp $dist:$jar_dir/* $pkg.ConvertFormat $output_dir/10-check-ds/ds-good.txt $output_dir/10-check-ds/latex/ds-good.tex --0s ds --0t ssf --0f sgml --1s ds --1t latex --1f latex
java -cp $dist:$jar_dir/* $pkg.ConvertFormat $output_dir/10-check-ds/ds-bad.txt $output_dir/10-check-ds/latex/ds-bad.tex --0s ds --0t ssf --0f sgml --1s ds --1t latex --1f latex

echo 
echo %%
echo %% 10 Checking PS Format
echo %%
if [ -d $output_dir/10-check-ps ]; then
	rm -rf $output_dir/10-check-ps
fi
mkdir $output_dir/10-check-ps
mkdir $output_dir/10-check-ps/latex
java -cp $dist:$jar_dir/* $pkg.CheckPSFormat $output_dir/00-ref-gloss/ps-gloss.txt $output_dir/10-check-ps/ps-good.txt $output_dir/10-check-ps/ps-bad.txt --0t $ps_tree_format --0f sgml --1f sgml --2f sgml 2> $output_dir/10-check-ps/errors.txt

java -cp $dist:$jar_dir/* $pkg.ConvertFormat $output_dir/10-check-ps/ps-good.txt $output_dir/10-check-ps/latex/ps-good.tex --0s ps --0t ssf --0f sgml --1s ps --1t latex --1f latex
java -cp $dist:$jar_dir/* $pkg.ConvertFormat $output_dir/10-check-ps/ps-bad.txt $output_dir/10-check-ps/latex/ps-bad.tex --0s ps --0t ssf --0f sgml --1s ps --1t latex --1f latex


echo
echo %% 11 Projectivizing all non-projective DS trees
if [ -d $output_dir/11-proj-ds ]; then
    rm -rf $output_dir/11-proj-ds
fi
mkdir $output_dir/11-proj-ds
mkdir $output_dir/11-proj-ds/latex
java -cp $dist:$jar_dir/* $pkg.MakeDSProjective $output_dir/10-check-ds/ds-good.txt $output_dir/11-proj-ds/ds-good-proj.txt $output_dir/11-proj-ds/ds-good-nonproj.txt --0s ds --0t ssf --0f sgml --1t ssf --1f sgml --2t ssf --2f sgml 2> $output_dir/11-proj-ds/errors.txt

java -cp $dist:$jar_dir/* $pkg.ConvertFormat $output_dir/11-proj-ds/ds-good-proj.txt $output_dir/11-proj-ds/latex/ds-good-proj.tex --0s ds --0t ssf --0f sgml --1s ds --1t latex --1f latex

java -cp $dist:$jar_dir/* $pkg.ConvertFormat $output_dir/11-proj-ds/ds-good-nonproj.txt $output_dir/11-proj-ds/latex/ds-good-nonproj.tex --0s ds --0t ssf --0f sgml --1s ds --1t latex --1f latex

echo 
echo %%
echo %% 20 Merging DS and PS
echo %%
if [ -d $output_dir/20-merge ]; then
	rm -rf $output_dir/20-merge
fi
mkdir $output_dir/20-merge
mkdir $output_dir/20-merge/latex
java -cp $dist:$jar_dir/* $pkg.MergeDSandPS $output_dir/10-check-ds/ds-good.txt $output_dir/10-check-ps/ps-good.txt $output_dir/20-merge/cs-good.txt --0t $ps_tree_format --0f sgml --1t $ds_tree_format --1f sgml --2t ssf --2f sgml --config $config_file 2> $output_dir/20-merge/errors.txt

java -cp $dist:$jar_dir/* $pkg.ConvertFormat $output_dir/20-merge/cs-good.txt $output_dir/20-merge/latex/cs-good.tex --0s cs --0t ssf --0f sgml --1s cs --1t latex --1f latex

echo 
echo %%
echo %% 25 Attaching PS empty categories to DS
echo %%
if [ -d $output_dir/25-attach-traces ]; then
	rm -rf $output_dir/25-attach-traces
fi
mkdir $output_dir/25-attach-traces
mkdir $output_dir/25-attach-traces/latex
java -cp $dist:$jar_dir/* $pkg.AttachTracesToDS $output_dir/20-merge/cs-good.txt $output_dir/25-attach-traces/cs-plus.txt --0t ssf --0f sgml --1t ssf --1f sgml 2> $output_dir/25-attach-traces/errors.txt
java -cp $dist:$jar_dir/* $pkg.ConvertFormat $output_dir/25-attach-traces/cs-plus.txt $output_dir/25-attach-traces/latex/cs-plus.tex --0s cs --0t ssf --0f sgml --1s cs --1t latex --1f latex

echo 
echo %%
echo %% 30 Checking consistency between DS and PS
echo %%
if [ -d $output_dir/30-check-consist ]; then
	rm -rf $output_dir/30-check-consist
fi
mkdir $output_dir/30-check-consist
mkdir $output_dir/30-check-consist/latex
java -cp $dist:$jar_dir/* $pkg.CheckConsistency $output_dir/25-attach-traces/cs-plus.txt $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/30-check-consist/cs-plus.inconst.txt --0t ssf --0f sgml --1t ssf --1f sgml --2t ssf --2f sgml --config $config_file 2> $output_dir/30-check-consist/errors.txt

java -cp $dist:$jar_dir/* $pkg.CheckConsistency $output_dir/25-attach-traces/cs-plus.txt $output_dir/30-check-consist/latex/cs-plus.consist.tex $output_dir/30-check-consist/latex/cs-plus.inconst.tex --0t ssf --0f sgml --1t latex --1f latex --2t latex --2f latex --config $config_file 2> $output_dir/30-check-consist/latex/errors.txt

#java -cp $dist:$jar_dir/* $pkg.ConvertFormat $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/30-check-consist/latex/cs-plus.consist.tex --0s cs --0t ssf --0f sgml --1s cs --1t latex --1f latex
#java -cp $dist:$jar_dir/* $pkg.ConvertFormat $output_dir/30-check-consist/cs-plus.inconst.txt $output_dir/30-check-consist/latex/cs-plus.inconst.tex --0s cs --0t ssf --0f sgml --1s cs --1t latex --1f latex

echo 
echo %%
echo %% 40 Extracting unary conversion rules -- text version
echo %%
if [ -d $output_dir/40-extract-unary ]; then
	rm -rf $output_dir/40-extract-unary
fi
mkdir $output_dir/40-extract-unary
java -cp $dist:$jar_dir/* $pkg.ExtractUnaryRules $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/40-extract-unary/rules.txt --0t ssf --0f sgml --1u text --1f text --config $config_file 2> $output_dir/40-extract-unary/errors.txt

echo
echo %%
echo %% 40 Extracting unary conversion rules -- latex version -- ambiguous only
echo %%
mkdir $output_dir/40-extract-unary/latex
java -cp $dist:$jar_dir/* $pkg.ExtractUnaryRules $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/40-extract-unary/latex/rules.tex --0t ssf --0f sgml --1u latex --1f latex --config $config_file -r 2> $output_dir/40-extract-unary/latex/errors.txt

echo
echo %%
echo %% 41 Extracting DS-PS POS mapping rules -- text version
echo %%
if [ -d $output_dir/41-extract-POS-map ]; then
	rm -rf $output_dir/41-extract-POS-map
fi
mkdir $output_dir/41-extract-POS-map
java -cp $dist:$jar_dir/* $pkg.ExtractUnaryRules $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/41-extract-POS-map/rules.txt --0t ssf --0f sgml --1u text --1f text --feats ectype --config $config_file -p 2> $output_dir/41-extract-POS-map/errors.txt

echo
echo %%
echo %% 41 Extracting DS-PS POS mapping rules --latex version -- ambiguous only
echo %%
mkdir $output_dir/41-extract-POS-map/latex
java -cp $dist:$jar_dir/* $pkg.ExtractUnaryRules $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/41-extract-POS-map/latex/rules.tex --0t ssf --0f sgml --1u latex --1f latex --feats ectype --config $config_file -p -r 2> $output_dir/41-extract-POS-map/errors.txt

echo
echo %%
echo %% 50 Extracting binary conversion rules -- text version
echo %%
if [ -d $output_dir/50-extract-binary ]; then
	rm -rf $output_dir/50-extract-binary
fi
mkdir $output_dir/50-extract-binary
java -cp $dist:$jar_dir/* $pkg.ExtractBinaryRules $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/50-extract-binary/rules.txt --0t ssf --0f sgml --1b text --1f text --config $config_file 2> $output_dir/50-extract-binary/errors.txt

echo
echo %%
echo %% 50 Extracting binary conversion rules -- latex version -- ambiguous only
echo %%
mkdir $output_dir/50-extract-binary/latex
java -cp $dist:$jar_dir/* $pkg.ExtractBinaryRules $output_dir/30-check-consist/cs-plus.consist.txt $output_dir/50-extract-binary/latex/rules.tex --0t ssf --0f sgml --1b latex --1f latex --config $config_file -r 2> $output_dir/50-extract-binary/latex/errors.txt

if $compile_latex; then
	$latex_dir/scripts/compile-latex-under-dir.sh $output_dir
fi
